<template>
  <div class="box-shadow">
    <!-- 搜索 -->
    <div class="search-area">
      <div class="form">
        <el-form
          :model="listQuery"
          ref="listQuery"
          size="small"
          :inline="true"
          label-width="115px"
        >
          <el-form-item :label="$t('巡检对象名称') + ':'" prop="deviceName" label-width="150px">
            <el-input
              v-model="listQuery.deviceName"
              :placeholder="$t('请输入')"
              clearable
              size="medium"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item
            :label="$t('识别类型') + ':'"
            prop="identifyType"
            label-width="140px"
          >
            <el-select
              v-model="listQuery.identifyType"
              :placeholder="$t('请选择')"
              clearable
              size="medium"
            >
              <el-option
                v-for="dict in dict.type.recognition"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('告警等级') + ':'">
            <el-checkbox-group v-model="listQuery.alarmLevelList">
              <el-checkbox label="4">{{ $t("危急告警") }}</el-checkbox>
              <el-checkbox label="3">{{ $t("严重告警") }}</el-checkbox>
              <el-checkbox label="2">{{ $t("一般告警") }}</el-checkbox>
              <el-checkbox label="1">{{ $t("预警") }}</el-checkbox>
              <el-checkbox label="0">{{ $t("正常") }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <!-- <el-form-item :label="$t('审核状态') + ':'">
            <el-checkbox-group v-model="listQuery.checkStatusList">
              <el-checkbox label="0">{{ $t("未审核") }}</el-checkbox>
              <el-checkbox label="1">{{ $t("已审核") }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item> -->
        </el-form>
      </div>
      <div class="search-area-btn btns">
        <div @click="serachBtn" class="serch-btn cuver-point">
          {{ $t("搜索") }}
        </div>
        <div @click="resetFormBtn('listQuery')" class="reset-btn cuver-point">
          {{ $t("重置") }}
        </div>
      </div>
    </div>
    <!-- 表格 -->
    <div class="table">
      <div class="self">
        <div class="table-title">
          <div class="taskName">
            <div class="label">{{ $t("任务名称") + "：" }}</div>
            <div class="value">{{ taskName }}</div>
          </div>
          <div class="pointName">
            <div class="label">{{ $t("巡检点名称") + "：" }}</div>
            <div class="value">{{ viewPointName }}</div>
          </div>
        </div>
        <el-table
          ref="tableRef"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%; margin-top: 16px"
          @selection-change="selectionChange"
        >
          <!-- <el-table-column width="55" type="selection"> </el-table-column> -->
          <el-table-column
            align="center"
            prop="deviceName"
            :label="$t('巡检对象名称')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="createTime"
            :label="$t('识别时间')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recognitionName"
            :label="$t('识别类型')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="deviceResult"
            :label="$t('识别结果')"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.deviceResult != null">{{
                scope.row.deviceResult + scope.row.unit
              }}</span>
              <span v-else>{{
                scope.row.deviceResult ? scope.row.deviceResult : "-"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="alarmLevelName"
            :label="$t('告警等级')"
          >
            <template slot-scope="scope">
              <div
                :class="[
                  scope.row.alarmLevelName == '危急告警' ? 'critical' : '',
                  scope.row.alarmLevelName == '严重告警' ? 'severity' : '',
                  scope.row.alarmLevelName == '一般告警' ? 'common' : '',
                  scope.row.alarmLevelName == '预警' ? 'early' : '',
                  scope.row.alarmLevelName == '正常' ? 'normal' : '',
                ]"
              >
                {{ scope.row.alarmLevelName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="pickInfo"
            :label="$t('采集信息')"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span
                v-for="(item, index) in scope.row.resourceType"
                :key="index"
                class="column-imgs"
              >
                <img
                  src="../../../assets/images/objectData/picture.png"
                  alt=""
                  @click="rowClick(scope.row, index, 'showPic')"
                  v-if="item == '0'"
                />
                <img
                  src="../../../assets/images/objectData/video.png"
                  alt=""
                  @click="rowClick(scope.row, index, 'showVideo')"
                  v-if="item == '1'"
                />
                <img
                  src="../../../assets/images/objectData/audio.png"
                  alt=""
                  @click="rowClick(scope.row, index, 'showAudio')"
                  v-if="item == '2'"
                />
                <span v-if="item == '-1' || item == ''">{{ $t("-") }}</span>
              </span>
            </template>
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="checkStatus"
            :label="$t('审核状态')"
          >
            <template slot-scope="scope">
              <div
                :style="{
                  color: scope.row.checkStatus == 0 ? '' : 'gray',
                }"
              >
                {{ scope.row.checkStatus == 0 ? $t("未审核") : $t(" 已审核") }}
              </div>
            </template>
          </el-table-column> -->
          <!-- <el-table-column align="center" label="操作" width="100">
            <template slot-scope="scope">
              <div class="table-btns">
                <div @click="handle(scope.row)" class="serch-btn cuver-point">
                  审核
                </div>
              </div>
            </template>
          </el-table-column> -->
        </el-table>
      </div>
      <div class="pagenation">
        <pagination
          :total="total"
          :page.sync="listQuerySubmit.pageNum"
          :limit.sync="listQuerySubmit.pageSize"
          @pagination="getTableList"
        >
        </pagination>
      </div>
    </div>
    <!-- dialog -->
    <el-dialog
      :title="$t('查看')"
      :visible.sync="dialogVisible"
      width="560px"
      @close="closeDialog"
    >
      <!-- 图片 -->
      <div
        v-if="
          dialogBooArr.find((item) => {
            return item.title == 'showPic';
          }).bool
        "
      >
        <div class="dialog-lineone">
          <div>{{ $t("拍摄时间") }}{{ dialogDetail.createTime }}</div>
          <div>{{ $t("文件名") }}{{ dialogDetail.resourceName }}</div>
          <div @click="downLoad" class="el-icon-download cursor-pointer"></div>
        </div>
        <div>
          {{ $t("图片") }}
          <img style="width: 100%" :src="dialogSrc" alt="" />
        </div>
      </div>
      <!-- 视频 -->
      <div
        v-if="
          dialogBooArr.find((item) => {
            return item.title == 'showVideo';
          }).bool
        "
      >
        <div class="dialog-lineone">
          <div>{{ $t("拍摄时间") }}{{ dialogDetail.createTime }}</div>
          <div>{{ $t("文件名") }}{{ dialogDetail.resourceName }}</div>
          <div @click="downLoad" class="el-icon-download cursor-pointer"></div>
        </div>
        <div>
          <video controls style="width: 100%" ref="showVideo">
            <source :src="dialogSrc" type="video/mp4" />
          </video>
        </div>
      </div>
      <!-- 音频 -->
      <div
        v-if="
          dialogBooArr.find((item) => {
            return item.title == 'showAudio';
          }).bool
        "
      >
        <div class="dialog-lineone">
          <div>{{ $t("拍摄时间") }}{{ dialogDetail.createTime }}</div>
          <div>{{ $t("文件名") }}{{ dialogDetail.resourceName }}</div>
          <div @click="downLoad" class="el-icon-download cursor-pointer"></div>
        </div>
        <div class="display:flex" v-if="currentAudioUrl.src != ''">
          <aplayer :music="currentAudioUrl" ref="showAudio"></aplayer>
          <div class="change-audio">
            <div class="reset-btn el-icon-arrow-left"></div>
            <div class="reset-btn el-icon-arrow-right"></div>
          </div>
        </div>
      </div>
      <div slot="footer" style="display: flex; justify-content: flex-end">
        <div class="reset-btn" @click="dialogVisible = false">
          {{ $t("取消") }}
        </div>
      </div>
    </el-dialog>

    <!-- 抽屉盒子 -->
    <el-drawer
      :title="$t('审核')"
      :visible.sync="drawerVisible"
      direction="rtl"
      :before-close="handleClose"
    >
      <el-form
        :model="drawerForm"
        ref="drawerRef"
        :rules="formRules"
        :inline="true"
        label-width="125px"
      >
        <el-form-item :label="$t('任务名称')" prop="name">
          <el-input v-model="drawerForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item :label="$t('当前模板')" prop="templateName">
          <el-input
            v-model="drawerForm.templateName"
            disabled
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="巡检速度：" prop="speed">
          <el-radio-group v-model="drawerForm.speed">
            <el-radio label="1">{{ $t("低速") }}</el-radio>
            <el-radio label="2">{{ $t("中速") }}</el-radio>
            <el-radio label="3">{{ $t("高速") }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div class="demo-drawer__footer drawer-footer">
        <div @click="submitBtn" class="serch-btn">{{ $t("保存") }}</div>
        <div @click="drawerVisible = false" class="reset-btn">
          {{ $t("取消") }}
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
/**
 * 数据中心-巡检记录-巡检点维度-巡检对象信息
 */

import aplayer from "vue-aplayer";
import {
  API_devictpageInfoList,
  API_getResourceinfo,
} from "@/api/data/inspectrecords.js";
export default {
  name: "",
  props: {},
  dicts: ["recognition"],
  components: {
    aplayer,
  },
  data() {
    return {
      drawerVisible: false, //是否打开抽屉盒子
      sevePort: window.location.protocol + "//" + window.location.host,
      drawerForm: {
        name: "",
        templateName: "",
        speed: "",
      },
      formRules: {
        name: [
          {
            required: true,
            message: this.$t("任务名称不能为空"),
            trigger: "blur",
          },
        ],
        templateName: [
          { required: true, message: this.$t("模板不能为空"), trigger: "blur" },
        ],
        speed: [
          {
            required: true,
            message: this.$t("巡检速度不能为空"),
            trigger: "change",
          },
        ],
      },
      listQuery: {
        // 查询条件
        deviceName: null,
        identifyType: null,
        alarmLevelList: [],
        checkStatusList: [],
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [], // table数据
      total: 0, // 分页总数
      listQuerySubmit: {}, // 提交参数
      dialogSrc: "", //弹框URL
      dialogDetail: {}, //弹框图片数据
      currentAudioUrl: {
        src: "",
      }, //音频
      dialogVisible: false, //图片dialog
      //控制弹框展示组件
      dialogBooArr: [
        {
          title: "showPic",
          bool: false,
        },
        {
          title: "showVideo",
          bool: false,
        },
        {
          title: "showAudio",
          bool: false,
        },
      ],
      taskExecId: this.$route.query.taskExecId,
      taskName: this.$route.query.taskName,
      viewPointName: this.$route.query.viewPointName,
      viewPointId: this.$route.query.viewPointId,
      downLoadUrl: "",
    };
  },
  created() {
    this.listQuerySubmit = Object.assign({}, this.listQuery);
    this.getTableList();
  },
  computed: {},
  methods: {
    /** 重置按钮操作 */
    resetFormBtn(formName) {
      this.listQuery = {
        deviceName: null,
        identifyType: null,
        alarmLevelList: [],
        checkStatusList: [],
      };
      this.serachBtn();
    },
    /**搜索 */
    serachBtn() {
      this.listQuery.pageNum = 1;
      this.listQuerySubmit = Object.assign({}, this.listQuery);
      this.getTableList();
    },
    /** table多选 */
    selectionChange(selection) {
      this.ids = selection.map((item) => item.resourceId);
      this.single = selection.length !== 1;
    },
    /**采集资源数据 */
    getTableList() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      // 告警等级
      let alarmLevelList = this.listQuerySubmit.alarmLevelList;
      if (alarmLevelList && alarmLevelList.length > 0) {
        if (alarmLevelList.indexOf(",") == -1) {
          alarmLevelList = String(alarmLevelList);
        } else {
          alarmLevelList = alarmLevelList.join(",");
        }
      }
      // 审核状态
      let checkStatusList = this.listQuerySubmit.checkStatusList;
      if (checkStatusList && checkStatusList.length > 0) {
        if (checkStatusList.indexOf(",") == -1) {
          checkStatusList = String(checkStatusList);
        } else {
          checkStatusList = checkStatusList.join(",");
        }
      }
      let obj = {
        ...this.listQuerySubmit,
        alarmLevelList: alarmLevelList,
        checkStatusList: checkStatusList,
        taskExecId: this.taskExecId,
        viewPointId: this.viewPointId,
      };
      API_devictpageInfoList(obj)
        .then((res) => {
          if (res.code == 200) {
            if (res.rows.length > 0) {
              this.tableData = res.rows;
              this.tableData.forEach((item) => {
                let resourceType = item.resourceType || "";
                let resourceId = item.resourceId || "";
                item.resourceType = resourceType.split(",");
                item.resourceId = resourceId.split(",");
              });
            } else {
              this.tableData = [];
            }
            this.total = Number(res.total);
            this.listQuery = Object.assign({}, this.listQuerySubmit);
          }
        })
        .finally(() => {
          loading.close();
        });
    },
    // 处理
    handle() {
      this.drawerVisible = true;
    },
    /**表格点击视频音频事件 */
    rowClick(rowInfo, index, type) {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.dialogSrc = "";
      let id = rowInfo.resourceId[index];
      API_getResourceinfo({ id: id })
        .then((res) => {
          if (res.code == 200) {
            this.dialogVisible = true;
            if (res.data) {
              if (type == "showPic") {
                this.dialogSrc = this.sevePort + res.data.resourceUrl;
              } else if (type == "showVideo") {
                this.$nextTick(() => {
                  this.$refs.showVideo.src =
                    this.sevePort + res.data.resourceUrl;
                });
              } else if (type == "showAudio") {
                this.currentAudioUrl = {
                  src: this.sevePort + res.data.resourceUrl,
                  title: res.data.resourceName,
                };
              }
              this.dialogDetail = res.data;
              this.downLoadUrl = res.data.resourceUrl;
            } else {
              this.$message.error(this.$t("暂无数据"));
            }
          } else {
            this.dialogSrc = "";
            this.dialogDetail = {};
          }
        })
        .finally(() => {
          loading.close();
        });

      this.dialogBooArr.forEach((item) => {
        if (item.title == type) {
          item.bool = true;
        } else {
          item.bool = false;
        }
      });
    },
    downLoad() {
      this.$download.resource(this.downLoadUrl);
    },
    /**close dialog */
    closeDialog() {
      this.dialogVisible = false;
      this.dialogBooArr.forEach((item) => {
        if (item.title == "showPic") return;
        if (item.bool) {
          this.$refs[item.title].pause();
        }
      });
    },
    /**抽屉盒子关闭前 */
    handleClose() {
      this.drawerVisible = false;
      this.resetDrawerForm();
    },
    /**重置drawer表单内容 */
    resetDrawerForm() {
      this.drawerForm = {
        name: "",
        templateName: "",
        speed: "",
      };
    },
    /**保存 btn */
    submitBtn() {
      this.$refs["drawerRef"].validate((valid) => {
        if (valid) {
          this.drawerVisible = false;
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.table {
  width: 100%;
  height: 738px;
  padding: 24px;
  box-sizing: border-box;
  border-top: 1px dashed rgba(219, 255, 251, 0.2);
  position: relative;
  .btns {
    display: flex;
    width: 100%;
    justify-content: flex-start;
    div:nth-child(2) {
      margin: 0 16px;
    }
  }
  .table-btns {
    display: flex;
    div {
      display: flex;
      width: 46px;
      height: 28px;
      justify-content: center;
      align-items: center;
      font-size: 12px;
    }
    div:nth-child(2) {
      margin: 0 10px;
    }
    .gray {
      color: orange;
    }
    .green {
      color: #73f6af;
    }
  }
  .table-title {
    display: flex;
    .pointName,
    .taskName {
      display: flex;
      color: #ffffff;
      font-size: 16px;
      .label {
        opacity: 0.7;
      }
      .value {
        font-weight: bold;
      }
    }
    .taskName {
      margin-right: 30px;
    }
  }
  .pagenation {
    position: absolute;
    bottom: 30px;
    right: 20px;
  }
}
.column-imgs {
  img {
    width: 24px;
    height: 24px;
    cursor: pointer;
  }
}
.drawer-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  div:nth-child(2) {
    margin-left: 16px;
  }
}
.critical {
  color: #ff5562;
}
.severity {
  color: #ff7000;
}
.common {
  color: #ffad30;
}
.early {
  color: #cc77dc;
}
.normal {
  color: #73f6af;
}
/** 音频播放器*/
.aplayer {
  background-color: transparent;
  color: #fff;
  .aplayer-body {
    display: flex;
    flex-direction: column;
    .aplayer-pic {
    }
  }
}
/**弹框样式 */
.dialog-lineone {
  display: flex;
  margin: 0 0 20px 0;
  div:nth-child(1) {
    width: 50%;
  }
  div:nth-child(2) {
    width: 45%;
  }
  div:nth-child(3) {
    width: 5%;
  }
}
.change-audio {
  display: flex;
  margin-top: 20px;
  justify-content: center;
  div:nth-child(2) {
    margin-left: 20px;
  }
}

::v-deep {
  .el-date-editor.el-input,
  .el-date-editor.el-input__inner {
    width: 195px;
  }
}
::v-deep {
  .el-dialog:not(.is-fullscreen) {
    margin-top: 30vh !important;
  }
}
::v-deep {
  .el-tabs__item {
    color: rgba(255, 255, 255, 0.5);
  }
  .el-tabs__nav-wrap::after {
    background-color: rgba(241, 243, 249, 0.2) !important;
  }
  .el-tabs__nav-scroll {
    padding: 0 35px;
  }
  .el-tabs__item.is-active {
    color: rgba(0, 209, 255, 1);
  }
  .el-tabs__active-bar {
    width: 50px !important;
    left: 4px;
    background-color: rgba(0, 209, 255, 1);
  }
}
::v-deep {
  .el-dialog:not(.is-fullscreen) {
    margin-top: 30vh !important;
  }
  .el-checkbox {
    margin-right: 10px;
  }
  .self {
    .el-table__body-wrapper {
      width: 100%;
      max-height: 531px;
      overflow: auto;
    }
  }
}
</style>
